﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class FrmPhieuthanhtoan : PRE.catalog.FrmBase
    {
        DAL.PhieuthanhtoanDAL _dal = new DAL.PhieuthanhtoanDAL();
        BLL.PhieuthanhtoanBLL _bll = new BLL.PhieuthanhtoanBLL();
        public FrmPhieuthanhtoan()
        {
            InitializeComponent();
        }
        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus_PhieuTT(false);

            DataTable _dtb1 = _dal.Select_phieukham1();
            cbosttphieu.Properties.DataSource = _dtb1;
        //    cbongaykham.Properties.DataSource = _dtb1;

            string _ma = _bll.CreateID();
            txtmaphieuthanhtoan.Text = _ma;

            txttongtien.ResetText();
            
            dtengaylap.EditValue = _bll.GetDate();
            cbomanv.EditValue=-1;
            cbosttphieu.EditValue = -1;
            cbongaykham.EditValue = _bll.GetDate();
            radiotrangthaiphieuthanhtoan.EditValue = false;
            dtengaylap.Focus();



            base.PerformAdd();
        }

        

        /// <summary>
        /// Perform when click cancel button
        /// </summary>

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtmaphieuthanhtoan.Text.Trim() ;
            var o = new DAL.Entities.phieuthanhtoan()
            {
                sttphieu=(cbosttphieu.EditValue+"").Trim(),
                ngaykham=Convert.ToDateTime(cbongaykham.EditValue),
                trangthaiphieukham=false
            };
            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    _dal.update_phieukham(o);
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus_PhieuTT(false);
            dtengaylap.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            string maphieu = grvMain.GetFocusedRowCellValue("MAPHIEUTHANHTOAN") + "";
            DataTable dtb_report = _dal.Select_Report(maphieu);

            var frm = new Report.Report_Base();
            var xr = new Report.XRThanhtoan();

            xr.DataSource = dtb_report;
            frm.SetReport(xr);

            frm.Show();
            
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            ChangeStatus_PhieuTT();
            LoadData();
            DataTable _dtb1 = _dal.Select_phieukham();
            cbosttphieu.Properties.DataSource = _dtb1;
          //  cbongaykham.Properties.DataSource = _dtb1;
            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {                       
            if (IsAdd)
            {
                var o = new DAL.Entities.phieuthanhtoan()
                {
                    maphieuthanhtoan = txtmaphieuthanhtoan.Text.Trim(),
                    ngaylapphieu = Convert.ToDateTime(dtengaylap.EditValue),
                    tongtien =float.Parse(txttongtien.Text.Trim()),
                    sttphieu=(cbosttphieu.EditValue+"").Trim(),
                    ngaykham = Convert.ToDateTime(cbongaykham.EditValue),
                    manv = (cbomanv.EditValue + "").Trim(),
                    trangthaiphieukham=true,
                    trangthaiphieuthanhtoan=Convert.ToBoolean(radiotrangthaiphieuthanhtoan.EditValue)
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    _dal.update_phieukham(o);
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_PhieuTT(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_PhieuTT(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                //var _mabn = grvMain.GetFocusedRowCellValue("MaBN") + "";
                var o = new DAL.Entities.phieuthanhtoan()
                {
                    maphieuthanhtoan = txtmaphieuthanhtoan.Text.Trim(),
                    ngaylapphieu = Convert.ToDateTime(dtengaylap.EditValue),
                    tongtien = float.Parse(txttongtien.Text.Trim()),
                    sttphieu = (cbosttphieu.EditValue + "").Trim(),
                    ngaykham = Convert.ToDateTime(cbongaykham.EditValue),
                    manv = (cbomanv.EditValue + "").Trim(),
                    trangthaiphieukham = true,
                    trangthaiphieuthanhtoan = Convert.ToBoolean(radiotrangthaiphieuthanhtoan.EditValue)
                };


                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_PhieuTT();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_PhieuTT(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            txtmaphieuthanhtoan.Text = null;
            txttongtien.Text = null;
            
            dtengaylap.EditValue =_bll.GetDate();

            cbomanv.EditValue = -1;
            cbosttphieu.EditValue =-1;
            cbongaykham.EditValue = _bll.GetDate();
            radiotrangthaiphieuthanhtoan.EditValue = false;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtmaphieuthanhtoan.DataBindings.Clear();
            txttongtien.DataBindings.Clear();
            dtengaylap.DataBindings.Clear();
            cbomanv.DataBindings.Clear();
            cbosttphieu.DataBindings.Clear();
            cbongaykham.DataBindings.Clear();
            radiotrangthaiphieuthanhtoan.DataBindings.Clear();


            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtmaphieuthanhtoan.DataBindings.Add("EditValue", _dtb, ".MAPHIEUTHANHTOAN");
            txttongtien.DataBindings.Add("EditValue", _dtb, ".TONGTIEN");
            dtengaylap.DataBindings.Add("EditValue", _dtb, ".NGAYLAPPHIEU");
            cbomanv.DataBindings.Add("EditValue", _dtb, ".MA_NV");
            cbosttphieu.DataBindings.Add("EditValue", _dtb, ".STTPHIEU");
            cbongaykham.DataBindings.Add("EditValue", _dtb, ".NGAYKHAM");
            radiotrangthaiphieuthanhtoan.DataBindings.Add("EditValue", _dtb, ".TRANGTHAITHANHTOAN");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            dtengaylap.Properties.ReadOnly = isReadOnly;
            cbosttphieu.Properties.ReadOnly = isReadOnly;
            cbongaykham.Properties.ReadOnly = isReadOnly;
            cbomanv.Properties.ReadOnly = isReadOnly;
            radiotrangthaiphieuthanhtoan.Properties.ReadOnly = isReadOnly;
            

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //DataTable tmp = _dal3.Select_Current(DateTime.Now);
            //string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            _dtb = _dal.Select();


            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
            }

            base.LoadData();
        }
        #endregion
        private void FrmPhieuthanhtoan_Load(object sender, EventArgs e)
        {
            txtmaphieuthanhtoan.Properties.ReadOnly = true;
            txttongtien.Properties.ReadOnly = true;

            DataTable _dtb1 = _dal.Select_phieukham();
            cbosttphieu.Properties.DataSource = _dtb1;
            cbongaykham.Properties.DataSource = _dtb1;

            DataTable _dtb2 = _dal.Select_nhanvien();
            cbomanv.Properties.DataSource = _dtb2;

            PerformRefresh();
        }

        //private void cbongaykham_EditValueChanged(object sender, EventArgs e)
        //{
        //    var o = new DAL.Entities.phieuthanhtoan()
        //    {
        //        sttphieu=cbosttphieu.EditValue+"",
        //        ngaykham= Convert.ToDateTime(cbongaykham.EditValue)
        //    };
        //    txttongtien.Text = _dal.select_tongtiendoenthuoc(o);
        //}

        private void cbosttphieu_EditValueChanged(object sender, EventArgs e)
        {
            if (IsAdd)
            {
                string _sttphieu = (cbosttphieu.EditValue + "").Trim();
                

                cbongaykham.Properties.DataSource = _dal.Select_sttphieu(_sttphieu);
                cbongaykham.ItemIndex = 0;
            }
        }

        private void cbongaykham_EditValueChanged_1(object sender, EventArgs e)
        {
            if (IsAdd)
            {
                var o = new DAL.Entities.phieuthanhtoan()
                {
                    sttphieu = cbosttphieu.EditValue + "",
                    ngaykham = Convert.ToDateTime(cbongaykham.EditValue)
                };
                txttongtien.Text = _dal.select_tongtiendoenthuoc(o);
            }
        }
    }
}
